﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Windows.Ink;
using System.IO;

namespace C7
{
	/// <summary>
	/// Interaction logic for InternetModule.xaml
	/// </summary>
    public partial class MemoireModule : UserControl
    {
        private WindowOperations WindowOPs = new WindowOperations();
        public MemoireModule()
        {
            this.InitializeComponent();
            WindowOPs.TextBoxesToHandlers(LayoutRoot);
            WindowOPs.WindowToAnimationHandlers(LayoutRoot, true);
        }

        private void SetupInkCanvas()
        {
            InkHelper.MainWindow = this;
            InkHelper.EditingMode = InkCanvasEditingMode.Ink;
            InkHelper.StrokeBrush = new SolidColorBrush(Colors.Black);
            InkHelper.StrokeSize = (double)2;
            InkHelper.ProcessDirectory(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\InkSample");

            LoadInkStrokes();
        }

        DrawingAttributes inkAttributes;

        public void UpdateInkCanvas()
        {
            inkAttributes = new DrawingAttributes();

            if (InkHelper.StrokeBrush != null)
            {
                inkAttributes.Color = InkHelper.StrokeBrush.Color;
            }
            else
            {
                inkAttributes.Color = Colors.Black;
            }

            if (InkHelper.StrokeSize != 0)
            {
                inkAttributes.Height = InkHelper.StrokeSize;
                inkAttributes.Width = InkHelper.StrokeSize;
            }
            else
            {
                inkAttributes.Height = 2;
                inkAttributes.Width = 2;
            }

            DrawingArea.EditingMode = InkHelper.EditingMode;
            DrawingArea.DefaultDrawingAttributes = inkAttributes;
        }

        private void LoadInkStrokes()
        {
            try
            {
                FileStream myStream = new FileStream(InkHelper.SaveFile, FileMode.Open, FileAccess.Read);
                StrokeCollection strokes = new StrokeCollection(myStream);
                myStream.Close();

                DrawingArea.Strokes = strokes;
            }
            catch
            {

            }
        }

        private void SaveInkStrokes()
        {
            if (File.Exists(InkHelper.SaveFile))
            {
                File.Delete(InkHelper.SaveFile);
            }

            FileStream myStream = new FileStream(InkHelper.SaveFile, FileMode.CreateNew);

            DrawingArea.Strokes.Save(myStream);
            myStream.Close();
        }

        private void EraserClicked(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            InkHelper.EditingMode = InkCanvasEditingMode.EraseByPoint;
            DrawingArea.EraserShape = new RectangleStylusShape(20, 20);
            UpdateInkCanvas();
        }

        private void PenClicked(object sender, System.Windows.Input.MouseButtonEventArgs e)
        {
            InkHelper.EditingMode = InkCanvasEditingMode.Ink;
            UpdateInkCanvas();
        }

        private void SaveDrawing(object sender, EventArgs e)
        {
            SaveInkStrokes();
        }

        private void Image_MouseLeftButtonDown(object sender, MouseButtonEventArgs e)
        {
            DrawingArea.Strokes.Clear();
        }

        private void lbPreviousDrawings_MouseDoubleClick(object sender, MouseButtonEventArgs e)
        {
            //lbPreviousDrawings.SelectedItem
            //DrawingArea.Strokes = ;
        }

        private void DrawingArea_MouseDown(object sender, MouseButtonEventArgs e)
        {
            // = DrawingArea.Strokes;
        }
    }
}